home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.10 Oct 95 / C++ Exceptions folder / Samples / exceptions.cp next >
Encoding:
Text File  |  1995-06-10  |  4.9 KB  |  198 lines  |  [TEXT/MPCC]

  1. /*
  2.     EXAMPLE OF EXCEPTION HANDLING USING MW 6.0 - Kent Sandvik
  3.     5/24/95
  4.     
  5.     Fun with exception handlers for handling unexpected events inside a C++ framework.
  6.     
  7.     
  8.     TODO:
  9.         Add set_unexpected when this is supported.
  10.     
  11.     ISSUES:
  12.         Note this code requires a compiler that handles the basic C++ exception model
  13.             (MW 6.0 or later, Symantec C++ compiler that handles compiler level 
  14.             exception handling).
  15.  
  16. */
  17.  
  18. // ______________________________________________________________________________
  19. // INCLUDES
  20. #include <iostream>
  21.  
  22.  
  23. // ______________________________________________________________________________
  24. // DEFINES
  25. #define DEBUG true
  26. // #define CATCHEVERYTHING
  27. #define CATCHSPECIFIC
  28.  
  29.  
  30. // ______________________________________________________________________________
  31. // MACROS
  32.  
  33. // This macro will automatically fill in the __FILE__ and __LINE statements
  34. // when a throw is used.
  35.  
  36. #define THROWEXCEPTION(name, number) \
  37.     throw TMacException( (name), (number), __FILE__, __LINE__)
  38.     
  39. #define THROWSERIOUSEXCEPTION(name, number) \
  40.     throw TSeriousMacException( (name), (number), __FILE__, __LINE__)
  41.  
  42.  
  43. // ______________________________________________________________________________
  44. // EXCEPTION CLASSES
  45.  
  46. class TMacException 
  47. {
  48. public:
  49.     TMacException(const char *theMessage, const OSErr theError) :
  50.                     fMessage(theMessage), 
  51.                     fError(theError), 
  52.                     fFileName("NO FILE SPECIFIED"), 
  53.                     fLineNumber(0L) 
  54.                     {};
  55.                     
  56.     TMacException(const char *theMessage, const OSErr theError, const char *theFileName, const long theLineNumber) :
  57.                     fMessage(theMessage), 
  58.                     fError(theError), 
  59.                     fFileName(theFileName), 
  60.                     fLineNumber(theLineNumber) 
  61.                     {};
  62.                     
  63.     const char*     GetExceptionMessage(void)     { return fMessage;};
  64.     const OSErr    GetExceptionOSErr(void)        { return fError;};
  65.     const char*    GetExceptionFile(void)        { return fFileName;};
  66.     const long        GetExceptionLine(void)        { return fLineNumber;};
  67.     
  68. protected:
  69.     const OSErr     fError;
  70.     const char*     fMessage;
  71.     const char*     fFileName;
  72.     const long        fLineNumber;
  73. };
  74.  
  75.  
  76. class TSeriousMacException : public TMacException
  77. {
  78. public:
  79.     TSeriousMacException(const char *theMessage, const OSErr theError) :
  80.                     TMacException(theMessage, theError, "NO FILE SPECIFIED", 0L)
  81.                     {};
  82.                     
  83.     TSeriousMacException(const char *theMessage, const OSErr theError, const char *theFileName, const long theLineNumber) :
  84.                     TMacException(theMessage, theError, theFileName, theLineNumber)
  85.                     {};
  86.  
  87. };
  88.  
  89. // ______________________________________________________________________________
  90. // TERMINATE DEFAULT FUNCTIONS
  91.  
  92. void HandleTerminate(void);
  93. void HandleTerminate(void)
  94. {
  95.     cout << "This is the my own terminate function that I've installed!" << endl;
  96. }
  97.  
  98.  
  99. // ______________________________________________________________________________
  100. // USER CLASSES
  101.  
  102. class Foo {
  103.     public:
  104.         Foo();
  105.         void DoASillyThing(void);
  106. };
  107.  
  108.  
  109. Foo::Foo()
  110. {
  111.     // cout << "we are inside the Foo constructor" << endl;
  112.  
  113. }
  114.  
  115. void Foo::DoASillyThing(void)
  116. {
  117. // Uncomment the test you want to try out:
  118.  
  119.     // throw TMacException("We did a silly thing", noErr);
  120.     // throw TMacException("We did a silly thing", noErr, __FILE__, __LINE__);
  121.     // throw TSeriousMacException("We did a really serious, silly thing", noErr);
  122.     // throw TSeriousMacException("We did a really serious, silly thing", noErr, __FILE__, __LINE__);
  123.     
  124.     // THROWEXCEPTION("We did a silly thing", noErr);
  125.      THROWSERIOUSEXCEPTION("We did a really serious, silly thing", noErr);
  126.     // throw "This is an exception that we have not specified!";
  127. }
  128.  
  129.  
  130. class Bar {
  131.     public:
  132.         Bar();
  133. };
  134.  
  135.  
  136. Bar::Bar()
  137. {
  138.     // cout << "We are inside the Bar constructor" << endl;
  139.     throw TMacException("Inside the Bar constructor", noErr);
  140. }
  141.  
  142.  
  143. // ______________________________________________________________________________
  144. // MAIN
  145.  
  146. void main (void)
  147. {
  148.     Foo* aFoo;
  149.     Bar* aBar;
  150.     
  151.     // set_terminate(HandleTerminate);
  152.     
  153.     try 
  154.     {
  155.     aFoo = new Foo;
  156.     
  157.     aFoo->DoASillyThing();
  158.     
  159.     aBar = new Bar;
  160.     }
  161.  
  162.  
  163. // Our catch statements, note that these should be ordered from specific to generic, inheritance wise,
  164. // this because the first valid exception handler is called, and the others will be ignored.
  165.  
  166. #ifdef CATCHSPECIFIC
  167.     catch (TSeriousMacException &ex)
  168.     {
  169.         long dontWriteToNull;
  170. #ifdef DEBUG
  171.         cerr << "SERIOUS EXCEPTION: " << ex.GetExceptionMessage() << ",  OSErr: " << ex.GetExceptionOSErr() <<
  172.                 ", File: " << ex.GetExceptionFile() << ",  Line: " << ex.GetExceptionLine() << endl;
  173. #endif //DEBUG
  174.         // Use real Mac UI to signal about the seriousness to the user of the application.
  175.         Delay(3*60, &dontWriteToNull);
  176.         ExitToShell();
  177.     
  178.     }
  179.     
  180.     catch(TMacException &ex)
  181.     {
  182. #ifdef DEBUG
  183.         cerr << "EXCEPTION: " << ex.GetExceptionMessage() << ",  OSErr: " << ex.GetExceptionOSErr() <<
  184.                 ", File: " << ex.GetExceptionFile() << ", Line: " << ex.GetExceptionLine() << endl;
  185. #endif //DEBUG
  186.         // Use real Mac UI to signal about the problem to the user of the application.
  187.     }
  188. #endif // CATCHSPECIFIC
  189.  
  190.  
  191. #ifdef CATCHEVERYTHING    
  192.     catch (...) // catch everything
  193.     {
  194.         cerr <<"We catch every single exception here.\n";
  195.     }
  196. #endif // CATCHEVERYTHING
  197.  
  198. }